import { 
	ApiGetGoodsSearchList,
	ApiGetGoodsDetail,
	ApiGetGoodsAttr,
	ApiGetGoodsCollocation,
	ApiSearchlog
 } from '@/common/request/api/goods.js'
 import { goodsContorl } from '@/common/utils/goods.js'
import {
	GOODS_SEARCHLIST,
	LOADING_STATUS,
	GOODS_UPDATECARTNUM,
	GOODS_CLEARCART,
	GOODS_DETAIL,
	GOODS_ATTR,
	GOODS_COLLOCATION,
	GOODS_DETAIL_UPDATECARTNUM,
	GOODS_COLLOCATION_UPDATECARTNUM,
} from "../types.js"
import {
	IMG_BASEURL
} from "../../utils/config.js"
const state = {
	goodsSearchList:{
		records:[],
		data:[],
		current:0,
		pages:0,
		size:10,
		total:0
	},
	loadingStatus:'loading',
	goodsDetail:{},
	goodsAttr:{},
	goodsCollocation:[]
}
const actions = {
	// ��ȡ��Ʒ�б�
	async ActGetGoodsSearchList({commit},payload){
		commit('LOADING_STATUS','loading')
		if (payload.current===1) {
			commit('GOODS_SEARCHLIST','reset')	
		}
		const res = await ApiGetGoodsSearchList(payload)
		if(res.success){
			commit('GOODS_SEARCHLIST',res.data)
		}
	},
	async ActGetGoodsDetail({commit},payload){
		commit('LOADING_STATUS','loading')
		const res = await ApiGetGoodsDetail(payload)
		if(res.success){
			commit('GOODS_DETAIL',res.data)
		}
	},
	async ActGetGoodsAttr({commit},payload){
		const res = await ApiGetGoodsAttr(payload)
		if(res.success){
			let newData = []
			let goodsDescUrlList = []
			let goodsDescUrl = []
			res.data&&res.data.goodsImgList?res.data.goodsImgList.map((item,index)=>{
				newData.push(IMG_BASEURL + item)
			}):''
			if (res.data&&res.data.goodsDescUrl) {
				goodsDescUrl = res.data.goodsDescUrl.split(',')
			}
			goodsDescUrl.length>0?goodsDescUrl.map((item,index)=>{
				goodsDescUrlList.push(IMG_BASEURL + item)
			}):''
			commit('GOODS_ATTR',{...res.data,advList:newData,goodsDescUrlList})
		}
	},
	async ActGetGoodsCollocation({commit},payload){
		const res = await ApiGetGoodsCollocation(payload)
		if(res.success){
			commit('GOODS_COLLOCATION',res.data)
		}
	},
	async ActSearchlog({commit},payload){
		const res = await ApiSearchlog(payload)
		return res
	},
}
const mutations ={
	[GOODS_SEARCHLIST](state,data){
		if (data==='reset') {
			state.goodsSearchList = {
				records:[],
				data:[],
				current:1,
				pages:0,
				size:10,
				total:0
			}
		}else {
			if(data.current === 1){
				data.data = data.records
			}else{
				data.data = state.goodsSearchList.data.concat(data.records)
			}
			if(data.current*data.size<=data.total){
				state.loadingStatus = 'loadmore'
			}else{
				state.loadingStatus = "nomore"
			}
			data.data.map((item,index)=>{
				item = goodsContorl(item)
			})
			state.goodsSearchList = data
		}
	},
	[LOADING_STATUS](state, data){
		state.loadingStatus = data
	},
	[GOODS_UPDATECARTNUM](state, data){
		data.map((dataItem,dataIndex)=>{
			// �޸���Ʒ�б��еĹ��ﳵ����
			state.goodsSearchList.data.map((goodsItem, goodsIndex)=>{
				if(goodsItem.goodsId == dataItem.goodsId){
					goodsItem.goodsNum=dataItem.num
					return
				}
			})
		})
	},
	[GOODS_COLLOCATION_UPDATECARTNUM](state, data){
		data.map((dataItem,dataIndex)=>{
			// �޸���Ʒ�б��еĹ��ﳵ����
			state.goodsCollocation.map((goodsItem, goodsIndex)=>{
				if(goodsItem.goodsId == dataItem.goodsId){
					goodsItem.goodsNum=dataItem.num
					return
				}
			})
		})
	},
	[GOODS_CLEARCART](state, data){
		// ������Ʒ�б��еĹ��ﳵ����
		state.goodsSearchList.data.map((goodsItem, goodsIndex)=>{
			goodsItem.goodsNum=0
		})
		state.goodsSearchList.data.map((goodsItem, goodsIndex)=>{
			goodsItem.goodsNum=0
		})
		state.goodsCollocation.map((goodsItem, goodsIndex)=>{
			goodsItem.goodsNum=0
		})
		state.goodsDetail.goodsNum = 0
	},
	[GOODS_DETAIL](state,data){
		state.goodsDetail = goodsContorl(data)
	},
	[GOODS_DETAIL_UPDATECARTNUM](state,data){
		state.goodsDetail.goodsNum = data
	},
	[GOODS_ATTR](state,data){
		state.goodsAttr = data
	},
	[GOODS_COLLOCATION](state,data){
		data.records.map((item,index)=>{
			item = goodsContorl(item)
		})
		state.goodsCollocation = data.records
	},
}
const getters = {}

export default {
	state,
	mutations,
	actions,
	getters
}